
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Calendar Date Select Test Cases</title>
  <script src="../prototype.js" type="text/javascript"></script>
  <script src="../unittest.js" type="text/javascript"></script>
  <!-- other JavaScript includes -->
  <script src="../../public/javascripts/calendar_date_select/calendar_date_select.js" type="text/javascript"></script>
  
  <link rel="stylesheet" href="../../public/stylesheets/calendar_date_select/red.css" type="text/css" />
  <link rel="stylesheet" href="../test.css" type="text/css" />
</head>
<body>

<!-- Log output -->
<div id="testlog"> </div>

<!-- here go any elements you do the testing on -->
<div id="cds_test_div">
  <input type="text" id="cds_test" />
</div>

<!-- Tests -->
<script type="text/javascript" language="javascript">
// <![CDATA[
  new Test.Unit.Runner({
    setup: function() { with(this){
      $("cds_test").value="";
      $("cds_test").disabled = false;
      $("cds_test").readOnly = false;
    }},
    teardown: function() { with(this){
      if (typeof(cds)!="undefined") assert(cds.closed,"all tests should close their calendar control");
      Date.first_day_of_week = 0;
      Date.weekdays = $w("S M T W T F S")
    }},
    testMixedTime_feedDateWithAndWithoutTime_shouldSetAccordingly: function() { with(this) {
      $A(["January 2, 2007 5:00 pm", "January 2, 2007 0:00 pm"]).each(function(d) {
        $("cds_test").value=d;
        cds = new CalendarDateSelect($("cds_test"), {time: "mixed"});
        assertEqual(true, cds.use_time);
        cds.close();
      })
      
      $("cds_test").value="January 2, 2007";
      cds = new CalendarDateSelect($("cds_test"), {time: "mixed"});
      assertEqual(false, cds.use_time);
      cds.close();      
    }},
    testTodayNowButton_mixedMode_shouldSetUseTimeAccordingly: function() { with(this){
      cds = new CalendarDateSelect($("cds_test"), {time: "mixed"});
      today_now = $$(".cds_buttons a[href=#]");
      today_now[0].onclick();
      assertEqual(false, cds.use_time, "when clicking today link");
      
      today_now[1].onclick();
      assertEqual(true, cds.use_time, "should have been true when clicking now link");
      cds.close();
    }},
    test__selectMar_23_2008__shouldSelectCorrectDate: function() { with(this){
      $("cds_test").value = "Mar 1, 2008"
      cds = new CalendarDateSelect($("cds_test"), {time: "true"});
      cds.updateSelectedDate({month: 2 /*+1*/, day: 23, year: 2008})
      
      assertEqual("23", $$('td.selected div').first().innerHTML);
      cds.close();
    }},
    test_updateSelectedDate: function() { with(this){
      $("cds_test").value = "January 1, 2007"
      cds = new CalendarDateSelect($("cds_test"), {time: "mixed"});

      cds.use_time=true;
      cds.updateSelectedDate({hour: ""})
      assertEqual(false, cds.use_time, "when setting hour to ''");
      
      cds.use_time=true;
      cds.updateSelectedDate({minute: ""})
      assertEqual(false, cds.use_time, "when setting minute to ''");
      
      cds.use_time=false;
      cds.updateSelectedDate({hour: 0})
      assertEqual(true, cds.use_time, "when setting hour to 0");
      
      cds.use_time=false;
      cds.updateSelectedDate({minute: 0})
      assertEqual(true, cds.use_time, "when setting minute to 0");
      
      cds.close();
    }},
    test_navYear: function() { with(this){
      $("cds_test").value = "January 1, 2007";
      cds = new CalendarDateSelect($("cds_test"), {time: "mixed"});
      cds.navYear(1870);
      assertEqual(2007, cds.selected_date.getFullYear());
      assertEqual(1870, cds.date.getFullYear());
      cds.close();
    }},
    test_navMonth: function() { with(this){
      $("cds_test").value = "January 1, 2007";
      cds = new CalendarDateSelect($("cds_test"), {time: "mixed"});
      cds.navMonth(5);
      assertEqual(0, cds.selected_date.getMonth());
      assertEqual(5, cds.date.getMonth())
      cds.close();
    }},
    test_callBacks: function() { with(this){
      $("cds_test").up().build("div", {id: "global"});
      cds = new CalendarDateSelect($("cds_test"), {time: "mixed", 
        before_show: function() { $("global").before_show_called = true},
        after_show: function() { $("global").after_show_called = true},
        before_close: function() { $("global").before_close_called = true},
        after_close: function() { $("global").after_close_called = true},
        after_navigate: function() { $("global").after_navigate_called = true},
        onchange: function() { $("global").onchange_called = true},
      });
      cds.navMonth(1);
      cds.navMonth(10);
      cds.updateSelectedDate({month: 1, day: 3, year: 2007});
      cds.close();
      assert($('global').before_show_called, "before_show wasnt called");
      assert($('global').after_show_called, "after_show wasnt called");
      assert($('global').before_close_called, "before_close wasnt called");
      assert($('global').after_close_called, "after_close wasnt called");
      assert($('global').after_navigate_called, "after_navigate wasnt called");
      assert($('global').onchange_called, "onchange wasnt called");
      $("global").remove();
    }},
    test_disabledElement_cantUpdate: function() {with(this){
      $("cds_test").value = "May 10, 2007";
      $("cds_test").disabled = true;
      cds = new CalendarDateSelect($("cds_test"));
      
      runTest = function() {
        cds.updateSelectedDate({day: 1, month:0, year: 2007});
        assertEqual(10, cds.selected_date.getDate())
      }
      runTest();
      $("cds_test").disabled = false;
      $("cds_test").readOnly = true;
      runTest();
      
      $("cds_test").readOnly = false;
      cds.close();
    }},
    test_readOnlyElement_forcePopup_shouldUpdate: function() {with(this){
      $("cds_test").value = "May 10, 2007";
      $("cds_test").readOnly = true;
      cds = new CalendarDateSelect($("cds_test"), {popup: "force"});
      
      cds.updateSelectedDate({day: 1, month:0, year: 2007});
      assertEqual(1, cds.selected_date.getDate())
      
      $("cds_test").readOnly = false;
      cds.close();
    }},
    test_timePassed_notUsingTime_shouldIgnore: function() {with(this) {
      $("cds_test").value = "May 10, 2007 5:00 pm";
      cds = new CalendarDateSelect($("cds_test"), {time:false});
      assert(cds.options.get("close_on_click"), "should set close_on_click to true when not using time")
      assertEqual(false, cds.use_time, "When parsing time");
      cds.today();
      assert(cds.closed, "should close when clicking today");
      assert(!cds.use_time, "Should not use time when calling today");
      
      cds = new CalendarDateSelect($("cds_test"), {time:false});
      cds.updateSelectedDate({hours: 5, minutes:30}) // this will close the control
      assertEqual(false, cds.use_time, "When setting hour/minute");
      assert(cds.closed);
    }},
    test_setUseTime_true_notUsingTime_shouldIgnore: function() {with(this) {
      $("cds_test").value = "May 10, 2007 5:00 pm";
      cds = new CalendarDateSelect($("cds_test"), {time:false});
      assertEqual(false, cds.use_time, "When parsing time");
      cds.setUseTime(true); // this will close the control
      assert(! cds.use_time, "After calling setUseTime(true)");
      cds.close();
    }},
    test_setMinute_notOnInterval_usesFloor: function() {with(this){
      $("cds_test").value = "May 10, 2007 5:25 pm";
      cds = new CalendarDateSelect($("cds_test"), {time:true});
      cds.updateSelectedDate({hour: 1, minute: 4});
      assertEqual(0, cds.selected_date.getMinutes());
      assertEqual(1, cds.selected_date.getHours());
      
      cds.close();
    }},
    // this checks for a safari 2 bug where safari parses "" as a date back in 1969
    test_parseDate_blankInput_interpretsAsNotSelected: function() {with(this){
      $("cds_test").value = " ";
      cds = new CalendarDateSelect($("cds_test"), {time:true});
      assertEqual(new Date().getFullYear(), cds.selected_date.getFullYear());
      assertEqual(new Date().getFullYear(), cds.date.getFullYear());
      assert(! cds.selection_made)
      cds.close();
    }},
    test_parseDate_timeTrue_noTimePassed_useTimeSetToTrueAnyway: function() {with(this){
      $("cds_test").value = "December 1, 2007 ";
      cds = new CalendarDateSelect($("cds_test"), {time:true});
      assert(cds.use_time)
      cds.close();
    }},
    test_passDivElement__shouldUseChildInputForTargetElement: function() {with(this){
      cds = new CalendarDateSelect($("cds_test_div"), {time:true});
      assertEqual("INPUT", cds.target_element.nodeName)
      cds.close();
    }},
    test__flexibleYearRange__datePassedWayBackWhen__shouldInitYearSelectAroundDate: function() {with(this){
      $("cds_test").value = "July 4, 1776";
      cds = new CalendarDateSelect($("cds_test_div"), {time:true});
      assert(cds.flexibleYearRange(), " should have had a flexibleYearRange");
      assert(cds.year_select.setValue(1776), "Should have allowed me to select 1776");
      cds.close();
    }},
    test__nonflexibleYearRange__dateRangeAtEnds__shouldntAllowNavigationPassedBoundary: function() {with(this){
      $("cds_test").value = "January 1, 2007";
      cds = new CalendarDateSelect($("cds_test"), {time:true, year_range: [2007, 2007]});
      
      assert(! cds.navYear(2006), "should not allow year navigation outside of boundary");
      assert(! cds.navYear(2008), "should not allow year navigation outside of boundary");
      assert(! cds.navMonth(-1), "shouldnt allow me to go beyond boundary");
      assert(! cds.navMonth(13), "shouldnt allow me to go beyond boundary");
      assert(cds.navMonth(1), "should allow me to be in boundary");
      assert(cds.navYear(2007), "should allow me to be in boundary");
      cds.close();
    }},
    test__nonflexibleYearRange__shouldPopulateRange: function() {with(this){
      $("cds_test").value = "January 1, 2007";
      cds = new CalendarDateSelect($("cds_test"), {time:true, year_range: [2007, 2007]});
      assert(cds.year_select.setValue(2007), "should allow me to select 2007");
      cds.close();
    }},
    test__nonflexibleYearRange__initializedWithDateOutsideOfRange__shouldShowNavDateAsClosestToOutsideYear: function() {with(this){
      $("cds_test").value = "January 1, 1900";
      cds = new CalendarDateSelect($("cds_test"), {time:true, year_range: [1997, 2007]});
      assertEqual(1997, cds.date.getFullYear(), "When going lesser than year_range");
      cds.close();
      
      $("cds_test").value = "January 1, 2010";
      cds = new CalendarDateSelect($("cds_test"), {time:true, year_range: [1997, 2007]});
      assertEqual(2007, cds.date.getFullYear(), "When going greater than year_range");
      
      cds.close();
    }},
    test__nonflexibleYearRange__initializedWithDateInsideOfRange__shouldShowNavDateAsYear: function() {with(this){
      $("cds_test").value = "January 1, 2005";
      cds = new CalendarDateSelect($("cds_test"), {year_range: [1997, 2007]});
      assertEqual(2005, cds.date.getFullYear(), "When going lesser than year_range");
      cds.close();
    }},
    test__flexibleYearRange__goToBoundary__shouldRefreshRange: function() {with(this){
      $("cds_test").value = "January 1, 2007";
      cds = new CalendarDateSelect($("cds_test"), {year_range: 10});
      cds.navYear(1997);
      assert(cds.year_select.setValue(1987), "should have refreshed with 1997 as the center");
      cds.navYear(2017);
      assert(cds.year_select.setValue(2027), "should have refreshed with 2027 as the center");
      cds.close();
    }},
    test__should_show_iframe_only_in_ie: function() {with(this){
      cds = new CalendarDateSelect($("cds_test"));
      
      if (navigator.appName=="Microsoft Internet Explorer")
        assertEqual(1, $$('iframe.ie6_blocker').length)
      else
        assertEqual(0, $$('iframe.ie6_blocker').length)
      
      cds.close();
    }},
    test__should_populate_minute_box_according_to_minute_interval: function() {with(this){
      cds = new CalendarDateSelect($("cds_test"), {time: true, minute_interval: 10});
      assert(cds.minute_select.setValue(0), "should allow me to select 0 minutes" );
      assert(!cds.minute_select.setValue(5), "should NOT allow me to select 5 minutes");
      assert(cds.minute_select.setValue(10), "should allow me to select 10 minutes" );
      cds.close();
    }},
    test__shouldnt_leak_variables: function() {with (this) {
      var vars = $w("padded2 compare_date str minute hour value e above e_dim e_top e_left matched e_bottom e_height c_pos c_left c_top c_height left_px top_px top_div header_div body_div buttons_div footer_div bottom_div days_tbody row_number cell_index weekday that pre_days use_time m y hover_date x d o")
      vars.each(function(v) {eval(v + " = -100");});
      
      cds = new CalendarDateSelect($("cds_test"), {time: true});
      cds.navMonth(1);
      cds.navYear(2);
      cds.dayHover($$('.cds_body td').first());
      cds.updateSelectedDate({day:1, month:1, year:2007});
      cds.close();
      
      vars.each(function(v) {
        assert(-100 == eval(v), "Variable " + v + " was leaked");
      });
    }},
    test__valid_date__shouldnt_allow_selection_of_invalid_dates_and_should_apply_css: function() {with (this) {
      $('cds_test').value = "December 11, 2007";
      cds = new CalendarDateSelect($("cds_test"), {valid_date_check: function(date) { return date < new Date("December 13, 2007")}, time: "mixed"});
      assertEqual("December 11, 2007", $F('cds_test'));
      cds.updateSelectedDate({day:13, month:12-1, year:2007});
      assertEqual("December 11, 2007", $F('cds_test'), "Date should not have been allowed to be selected");
      cds.updateSelectedDate({day:10, month:12-1, year:2007});
      assertEqual("December 10, 2007", $F('cds_test'), "Date should not have been allowed to be selected");
      
      day_12_element = $$('.calendar_date_select td')[17];
      day_13_element = $$('.calendar_date_select td')[18];
      assertEqual(12, day_12_element.day); // make sure we have th right one
      assertEqual(13, day_13_element.day); // make sure we have th right one
      assert( ! day_12_element.hasClassName("disabled"), "Day 12 shouldnt be disabled");
      assert(day_13_element.hasClassName("disabled"), "Day 13 should be disabled");
      
      cds.close();
    }},
    test__valid_date_check_is_nil__should_show_all_cells_as_enabled: function() {with (this) {
      $('cds_test').value = "December 11, 2007";
      cds = new CalendarDateSelect($("cds_test"));
      
      $$('.calendar_date_select td').each(function(e){
        assert( ! e.hasClassName("disabled"));
      });
      
      cds.close();
    }},
    test__should_pull_up_dec_07_when_first_day_of_week_is_1: function() {with (this) {
      Date.first_day_of_week = 1;
      Date.weekdays = $w("M T W T F S S");
      $('cds_test').value = "December 1, 2007";
      cds = new CalendarDateSelect($("cds_test"));
      
      assertEqual(1, $$(".calendar_date_select").length);
      cds.close();
    }}
  });
// ]]>
</script>
</body>
</html>